package cn.edu.scau.cmi.ema.domain;


import java.io.Serializable;

import java.lang.StringBuilder;

import java.util.Calendar;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import javax.persistence.*;
import javax.xml.bind.annotation.*;
import cn.edu.scau.cmi.ema.domain.*;
import java.util.Set;
import com.alibaba.fastjson.annotation.JSONField;

@Entity
@NamedQueries({ @NamedQuery(name = "findAllUrls", query = "select myUrl from Url myUrl"),
		@NamedQuery(name = "findUrlByName", query = "select myUrl from Url myUrl where myUrl.name = ?1"),
		@NamedQuery(name = "findUrlByNameContaining", query = "select myUrl from Url myUrl where myUrl.name like concat('%',?1,'%')"),
		@NamedQuery(name = "findUrlByTitle", query = "select myUrl from Url myUrl where myUrl.title = ?1"),
		@NamedQuery(name = "findUrlByTitleContaining", query = "select myUrl from Url myUrl where myUrl.title like concat('%',?1,'%')"),
		@NamedQuery(name = "findUrlByDate", query = "select myUrl from Url myUrl where myUrl.date = ?1"),
		@NamedQuery(name = "findUrlByValue", query = "select myUrl from Url myUrl where myUrl.value = ?1"),
		@NamedQuery(name = "findUrlByValueContaining", query = "select myUrl from Url myUrl where myUrl.value like concat('%',?1,'%')"),
		@NamedQuery(name = "findUrlByContent", query = "select myUrl from Url myUrl where myUrl.content = ?1"),
		@NamedQuery(name = "findUrlByContentContaining", query = "select myUrl from Url myUrl where myUrl.content like concat('%',?1,'%')"),
		@NamedQuery(name = "findUrlByDescription", query = "select myUrl from Url myUrl where myUrl.description = ?1"),
		@NamedQuery(name = "findUrlByDescriptionContaining", query = "select myUrl from Url myUrl where myUrl.description like concat('%',?1,'%')"),
		@NamedQuery(name = "findAllUrlsBySite", query = "select myUrl from Url myUrl where myUrl.site.id = ?1"),
		@NamedQuery(name = "findAllUrlsByAuthor", query = "select myUrl from Url myUrl where myUrl.author.id = ?1"),
		@NamedQuery(name = "findUrlById", query = "select myUrl from Url myUrl where myUrl.id = ?1"),
		@NamedQuery(name = "findUrlByPrimaryKey", query = "select myUrl from Url myUrl where myUrl.id = ?1 "),
		@NamedQuery(name = "deleteUrlsByIds", query = "delete Url where id in (?1)"),
		@NamedQuery(name = "findUrlsByIds", query = "select myUrl from Url myUrl where myUrl.id in (?1)"),
		@NamedQuery(name = "findUrlByPhyscialPrimaryKey", query = "select myUrl from Url myUrl where  myUrl.value = ?1"),
		@NamedQuery(name = "countUrlsByName", query = "select count(myUrl)  from Url myUrl where myUrl.name like concat('%',?1,'%')") })
@Table(catalog = "ema", name = "url")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(namespace = "ema/cn/edu/scau/cmi/ema/domain", name = "Url")
@XmlRootElement(namespace = "ema/cn/edu/scau/cmi/ema/domain")

public class Url implements Serializable, Comparable<Url> {

	private static final long serialVersionUID = 1L;

	@Column(name = "id", nullable = false)
	@Basic(fetch = FetchType.EAGER)
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@XmlElement
	Integer id;

	@Column(name = "name")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	String name;

	@Column(name = "title")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	String title;

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "date")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	Calendar date;

	@Column(name = "value")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	String value;

	@Column(name = "content")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	String content;

	@Column(name = "description")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	String description;
	@JSONField(serialize = false)
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumns({ @JoinColumn(name = "site", referencedColumnName = "id", nullable = false) })
	@XmlTransient
	Site site;
	@JSONField(serialize = false)
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumns({ @JoinColumn(name = "author", referencedColumnName = "id", nullable = false) })
	@XmlTransient
	User author;
	@JSONField(serialize = false)
	@OneToMany(mappedBy = "url", cascade = { CascadeType.REMOVE }, fetch = FetchType.LAZY)
	@XmlElement(name = "", namespace = "")
	Set<Crawlerfile> crawlerfilesForUrl;
	@JSONField(serialize = false)
	@OneToMany(mappedBy = "url", cascade = { CascadeType.REMOVE }, fetch = FetchType.LAZY)
	@XmlElement(name = "", namespace = "")
	Set<Crawlerknowledge> crawlerknowledgesForUrl;
	@JSONField(serialize = false)
	@OneToMany(mappedBy = "url", cascade = { CascadeType.REMOVE }, fetch = FetchType.LAZY)
	@XmlElement(name = "", namespace = "")
	Set<Event> eventsForUrl;

	public void setId(Integer id) {
		this.id = id;
	}

	public Integer getId() {
		return this.id;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return this.name;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getTitle() {
		return this.title;
	}

	public void setDate(Calendar date) {
		this.date = date;
	}

	public Calendar getDate() {
		return this.date;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String getValue() {
		return this.value;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getContent() {
		return this.content;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getDescription() {
		return this.description;
	}

	public void setSite(Site site) {
		this.site = site;
	}

	public Site getSite() {
		return site;
	}

	public void setAuthor(User author) {
		this.author = author;
	}

	public User getAuthor() {
		return author;
	}

	public void setCrawlerfilesForUrl(Set<Crawlerfile> crawlerfilesForUrl) {
		this.crawlerfilesForUrl = crawlerfilesForUrl;
	}

	public Set<Crawlerfile> getCrawlerfilesForUrl() {
		if (crawlerfilesForUrl == null) {
			crawlerfilesForUrl = new java.util.LinkedHashSet<Crawlerfile>();
		}
		return crawlerfilesForUrl;
	}

	public void setCrawlerknowledgesForUrl(Set<Crawlerknowledge> crawlerknowledgesForUrl) {
		this.crawlerknowledgesForUrl = crawlerknowledgesForUrl;
	}

	public Set<Crawlerknowledge> getCrawlerknowledgesForUrl() {
		if (crawlerknowledgesForUrl == null) {
			crawlerknowledgesForUrl = new java.util.LinkedHashSet<Crawlerknowledge>();
		}
		return crawlerknowledgesForUrl;
	}

	public void setEventsForUrl(Set<Event> eventsForUrl) {
		this.eventsForUrl = eventsForUrl;
	}

	public Set<Event> getEventsForUrl() {
		if (eventsForUrl == null) {
			eventsForUrl = new java.util.LinkedHashSet<Event>();
		}
		return eventsForUrl;
	}

	public void copy(Url that) {

		setId(that.getId());

		setName(that.getName());
		setTitle(that.getTitle());
		setDate(that.getDate());
		setValue(that.getValue());
		setContent(that.getContent());
		setDescription(that.getDescription());

		setSite(that.getSite());

		setAuthor(that.getAuthor());

		setCrawlerfilesForUrl(
				new java.util.LinkedHashSet<Crawlerfile>(that.getCrawlerfilesForUrl()));

		setCrawlerknowledgesForUrl(new java.util.LinkedHashSet<Crawlerknowledge>(
				that.getCrawlerknowledgesForUrl()));

		setEventsForUrl(new java.util.LinkedHashSet<Event>(that.getEventsForUrl()));

	}

	public Url() {

	}

	@Override
	public String toString() {

		StringBuilder buffer = new StringBuilder();

		buffer.append("id=[").append(id).append("] ");

		buffer.append("name=[").append(name).append("] ");
		buffer.append("title=[").append(title).append("] ");
		buffer.append("date=[").append(date).append("] ");
		buffer.append("value=[").append(value).append("] ");
		buffer.append("content=[").append(content).append("] ");
		buffer.append("description=[").append(description).append("] ");

		return buffer.toString();
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = (int) (prime * result + ((id == null) ? 0 : id.hashCode()));
		return result;
	}

	public boolean equals(Object obj) {
		if (obj == this)
			return true;
		if (!(obj instanceof Url))
			return false;
		Url comparedUrl = (Url) obj;
		if (!CheckPropertyEqual(this.value, comparedUrl.value)) {
			return false;
		}
		return true;
	}

	public static <T> boolean CheckPropertyEqual(T originProperty, T comparedProperty) {
		// 基本类型经过参数传递后会自动装箱
		String[] className = originProperty.getClass().getName().split("\\.");
		if (className[0].equals("java") && className[1].equals("math")) {
			return ((Comparable) originProperty).compareTo(comparedProperty) == 0 ? true : false;
		} else {
			return originProperty.equals(comparedProperty);
		}
	}

	@Override
	public int compareTo(Url o) {
		if (this.getId() > o.getId()) {
			return 1;
		} else if (this.getId() == o.getId()) {
			return 0;
		} else {
			return -1;
		}

	}
}
